﻿let MyPlugin = {}
MyPlugin.install = function (Vue, options) {
  // 清除左右两边的空格
  Vue.prototype.$trim = function (str) {
    return str.replace(/(^\s*)|(\s*$)/g, '')
  }
  // 判断空对象
  Vue.prototype.$isEmptyObject = function (obj) {
    for (let attr in obj) {
      return false
    }
    return true
  }
  // 获取当前时间
  Vue.prototype.$getCurrentDate = function () {
    let separator = '-'
    const current = new Date ()
    let year = current.getFullYear()
    let month = (current.getMonth() + 1) < 10 ? ('0' + (current.getMonth() + 1)) : (current.getMonth() + 1)
    let day = current.getDate() < 10 ? ('0' + current.getDate()) : current.getDate()
    let hours = current.getHours() < 10 ? ('0' + current.getHours()) : current.getHours()
    let mins = current.getMinutes() < 10 ? ('0' + current.getMinutes()) : current.getMinutes()
    let seconds = current.getSeconds() < 10 ? ('0' + current.getSeconds()) : current.getSeconds()
    let time = hours + ':' + mins + ':' + seconds
    return year + separator + month + separator + day + ' ' + time
  }
  // 判断数组还是对象
  Vue.prototype.$isArray = function (value) {
    if (typeof Array.isArray === 'function') {
      return Array.isArray(value)
    } else {
      return Object.prototype.toString.call(value) === '[object Array]'
    }
    // value.constructor.toString().indexOf('Array') > -1
  }
  // 替换eval()方法，避免在控制台报错
  Vue.prototype.$selfdefineEval = function (fn) {
    let Fn = Function
    return new Fn('return ' + fn)()
  }
  // 将数据结构转化为树形结构
  Vue.prototype.$toTree = function (data, toArray) {
    let dataArray = []
    data.forEach(function (item) {
      if (toArray) {
        if (!item.p_functionno) {
          const objTem = {
            functionname: item.functionname,
            functionno: item.functionno,
            functionurl: item.functionurl,
            p_functionno: item.p_functionno,
            icon: item.icon,
            is_menu: item.is_menu,
            sortnum: item.sortnum,
            action: item.action,
            target: item.target,
            url_type: item.url_type
          }
          dataArray.push(objTem)
        }
      } else {
        if (!item.p_function_no) {
          const objTem = {
            function_name: item.function_name,
            function_no: item.function_no,
            function_url: item.function_url,
            p_function_no: item.p_function_no,
            sortnum: item.sort_num,
            action: item.action,
            target: item.target,
            icon: item.icon,
            url_type: item.url_type
          }
          dataArray.push(objTem)
        }
      }
    })
    return dataToTree(data, dataArray, toArray)
  }
  function dataToTree (data, dataArray, toArray) {
    dataArray.forEach(function (pItem) {
      const childrenArray = []
      data.forEach(function (sItem) {
        if (toArray) {
          if (sItem.p_functionno === pItem.functionno) {
            var objTem = {
              functionname: sItem.functionname,
              functionno: sItem.functionno,
              functionurl: sItem.functionurl,
              p_functionno: sItem.p_functionno,
              icon: sItem.icon,
              is_menu: sItem.is_menu,
              sortnum: sItem.sortnum,
              action: sItem.action,
              target: sItem.target,
              url_type: sItem.url_type
            }
            childrenArray.push(objTem)
          }
        } else {
          if (sItem.p_function_no === pItem.function_no) {
            var objTems = {
              function_name: sItem.function_name,
              function_no: sItem.function_no,
              function_url: sItem.function_url,
              p_function_no: sItem.p_function_no,
              sortnum: sItem.sort_num,
              action: sItem.action,
              icon: sItem.icon,
              target: sItem.target,
              url_type: sItem.url_type
            }
            childrenArray.push(objTems)
          }
        }
      })
      pItem.children = childrenArray
      if (childrenArray.length > 0) {
        dataToTree(data, childrenArray, toArray)
      }
    })
    return dataArray
  }
  // 注册一个全局自定义指令 `v-drag`
  Vue.directive('drag', {
    bind(el, binding, vnode, oldVnode) {
      const dialogHeaderEl = el.querySelector('.el-dialog__header')
      const dragDom = el.querySelector('.el-dialog')
      //dialogHeaderEl.style.cursor = 'move';
      dialogHeaderEl.style.cssText += ';cursor:move;'
      dragDom.style.cssText += ';top:0px;'
      // 获取原有属性 ie dom元素.currentStyle 火狐谷歌 window.getComputedStyle(dom元素, null)
      const sty = (function() {
        if (window.document.currentStyle) {
          return (dom, attr) => dom.currentStyle[attr]
        } else {
          return (dom, attr) => getComputedStyle(dom, false)[attr]
        }
      })()
      dialogHeaderEl.onmousedown = (e) => {
        // 鼠标按下，计算当前元素距离可视区的距离
        const disX = e.clientX - dialogHeaderEl.offsetLeft
        const disY = e.clientY - dialogHeaderEl.offsetTop
        const screenWidth = document.body.clientWidth // body当前宽度
        const screenHeight = document.documentElement.clientHeight // 可见区域高度(应为body高度，可某些环境下无法获取) 
        const dragDomWidth = dragDom.offsetWidth // 对话框宽度
        const dragDomheight = dragDom.offsetHeight // 对话框高度
        const minDragDomLeft = dragDom.offsetLeft
        const maxDragDomLeft = screenWidth - dragDom.offsetLeft - dragDomWidth
        const minDragDomTop = dragDom.offsetTop
        const maxDragDomTop = screenHeight - dragDom.offsetTop - dragDomheight
        // 获取到的值带px 正则匹配替换
        let styL = sty(dragDom, 'left')
        let styT = sty(dragDom, 'top')
        // 注意在ie中 第一次获取到的值为组件自带50% 移动之后赋值为px
        if (styL.includes('%')) {
          styL = +document.body.clientWidth * (+styL.replace(/\%/g, '') / 100)
          styT = +document.body.clientHeight * (+styT.replace(/\%/g, '') / 100)
        } else {
          styL = +styL.replace(/\px/g, '')
          styT = +styT.replace(/\px/g, '')
        }
        document.onmousemove = function (e) {
        // 通过事件委托，计算移动的距离
          let left = e.clientX - disX
          let top = e.clientY - disY
          // 边界处理
          if (-(left) > minDragDomLeft) {
            left = -(minDragDomLeft)
          } else if (left > maxDragDomLeft) {
            left = maxDragDomLeft
          }
          if (-(top) > minDragDomTop) {
            top = -(minDragDomTop)
          } else if (top > maxDragDomTop) {
            top = maxDragDomTop
          }
          // 移动当前元素
          dragDom.style.cssText += `;left:${left + styL}px;top:${top + styT}px;`
        }
        document.onmouseup = function (e) {
          document.onmousemove = null
          document.onmouseup = null
        }
      }  
    }
  })
  // 注册一个时间的过滤器
  Vue.filter('dateTimeToDate', (value, separator) => {
    separator = separator || '-'
    if (!value) {
      return  ''
    }
    const current = new Date (value)
    let year = current.getFullYear()
    let month = (current.getMonth() + 1) < 10 ? ('0' + (current.getMonth() + 1)) : (current.getMonth() + 1)
    let day = current.getDate() < 10 ? ('0' + current.getDate()) : current.getDate()
    return year + separator + month + separator + day
  })
  Vue.filter('formatterDateToYear', (value) => {
    if (!value) {
      return  ''
    }
    const current = new Date (value)
    return current.getFullYear()
  })
  // 注册一个图片视频的地址的过滤器
  Vue.filter('srcFormatting', (value) => {
    if (!value) {
      return  ''
    }
    if (value.indexOf('/name/') !== -1) {
      value = value.slice(0, value.indexOf('/name/'))
    }
    if (value.indexOf('/dl/') !== -1) {
      value = value.replace('/dl/', '/')
    }
    return value
  })
}

export default MyPlugin
